package com.twt.recruit.service;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.twt.recruit.entity.ApplicationComment;
import com.twt.recruit.entity.TimeHand;
import com.twt.recruit.entity.vo.TimeSlot;
import com.twt.recruit.mapper.*;
import com.twt.recruit.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RecruitService {

    @Resource
    TimeMapper timeMapper;

    @Resource
    TimeHandMapper timeHandMapper;

    @Resource
    ProjectMapper projectMapper;

    @Resource
    ApplicationMapper applicationMapper;

    @Resource
    CommentMapper commentMapper;

    @Value("${times}")
    String TIME;


    public void interviewSchedule(Integer day, Integer projectId){
        List<TimeSlot> timeSlots = timeMapper.listTimeSlotsByProjectId(projectId);
        // 创建 InterviewScheduler 实例并调度面试
        InterviewScheduler scheduler = new InterviewScheduler(timeSlots);
        List<List<TimeSlot>> scheduledInterviews = scheduler.scheduleInterviews(day);

        // 输出已调度的面试时间

        for (int i = 0; i < scheduledInterviews.size(); i++) {
            Map<String, Object> dayMap = new HashMap<>();
            dayMap.put("day", i + 1);

            List<TimeSlot> timeSlotList = scheduledInterviews.get(i);
            for (TimeSlot timeSlot : timeSlotList) {
                TimeHand timeHand = new TimeHand(timeSlot.getApplicationId(), projectId, i+1, timeSlot.getTimeSection(), (byte) 0);
                if (timeHandMapper.selectById(timeSlot.getApplicationId())==null){
                    timeHandMapper.insert(timeHand);
                }else {
                    timeHandMapper.updateById(timeHand);
                }
            }
        }
    }


    public JSONObject getAuto(Integer projectId){
        List<List<Integer>> times = new ArrayList<>();
        String[] timeIndex = TIME.split(",");
        for (String index : timeIndex) {
            List<Integer> count = new ArrayList<>();
            for (int i = 1; i <= 3; i++) {
                count.add(timeHandMapper.listTimeSectionCount(projectId, i, index));
            }
            times.add(count);
        }
        JSONObject re = new JSONObject();
        re.putOpt("newCount", timeHandMapper.listNewApplicationCount(projectId));
        re.putOpt("update", TimeUtil.timestampToDate(timeHandMapper.getLatestTime(projectId).getTime(), TimeUtil.Y_M_D_H_M_S));
        re.putOpt("times", times);
        re.putOpt("startTime", TimeUtil.timestampToDate(projectMapper.selectById(projectId).getStartTime().getTime(), TimeUtil.Y_M_D));
        return re;
    }

    public JSONObject dealCategory(Integer applicationId){
        JSONObject t = new JSONObject();
        t.putOpt("applicationId", applicationId);
        t.putOpt("applicationName", applicationMapper.selectById(applicationId).getApplicationName());
        LambdaQueryWrapper<ApplicationComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApplicationComment::getApplicationId, applicationId);
        queryWrapper.orderByDesc(ApplicationComment::getUpdatedTime);
        ApplicationComment applicationComment = commentMapper.selectOne(queryWrapper);
        t.putOpt("comment",applicationComment==null?"":applicationComment.getCommentContent());
        if (applicationComment!=null){
            t.putOpt("commentTime", TimeUtil.timestampToDate(applicationComment.getUpdatedTime().getTime(), TimeUtil.Y_M_D_H_M));
        }else {
            t.putOpt("commentTime","");

        }
        return t;
    }





}
